#include"searcher.hpp"
#include<string>
#include"cpp-httplib/httplib.h"
#include"log.hpp"

const std::string root_path = "./wwwroot";
const std::string src_path = "./data/parse_html/parse.txt";

int main()
{
    ns_searcher::Searcher search;
    search.InitSearcher(src_path);
    httplib::Server svr;
    //设置web根目录路径
    svr.set_base_dir(root_path.c_str());
    svr.Get("/s",[&search](const httplib::Request& resq,httplib::Response& resp)
    {
        //检查请求包中的参数
        if(!resq.has_param("word"))
        {
            //如果没有word参数就返回
            resp.set_content("请输入需要搜索的关键字","text/plain; charset=utf-8");
            return;
        }
        //获取请求端的word参数值
        std::string word = resq.get_param_value("word");
        LOG(INFO,"用户正在搜索: " + word);
        //std::cout << "用户正在搜索: " << word << std::endl;
        std::string json_str;
        //查找word关键之并返回我们的搜索结果
        search.Search(word,&json_str);
        //返回响应
        resp.set_content(json_str,"application/json; charset:utf-8");
        
    });

    LOG(INFO,"服务器启动成功...");
    //设置监听ip和端口
    svr.listen("0.0.0.0",8888);
    return 0;
}